<?php
include_once('../dao/BankLoanDAO.php');
include_once('../entitys/BankLoan.php');
include_once('../dao/BankDAO.php');
require_once('RedirectForward.php');

$url = "admin/admin.php?content=quanlivay";

if(isset($_POST['acc'])) {
	$acction = $_POST['acc'];
	if($acction == 'add') {
		if(isset($_POST['loanid']) && isset($_POST['bankloadselect']) && isset($_POST['dactinh']) && isset($_POST['dieukien']) && isset($_POST['thutuc'])) {
			$id = time();
			$loanid = $_POST['loanid'];
			$bankid = $_POST['bankloadselect'];
			$bankarr = getBank($bankid);
			if(count($bankarr) > 0) {
				$bankloan = new BankLoan();
				$bankloan -> setID($id);
				$bankloan -> setBank($bankid);
				$bankloan -> setLoan($loanid);
				$bankloan -> setDacTinh($_POST['dactinh']);
				$bankloan -> setDieuKien($_POST['dieukien']);
				$bankloan -> setThuTuc($_POST['thutuc']);
				$bankloan -> setBankName($bankarr[0][1]);
				$bankloan -> setBankLogo($bankarr[0][2]);
				$bankloan -> setDatecreate($id);
				
				$url .= "&&as=".$loanid."&&adrt=addbankloan";
				if(insertBankLoan($bankloan)) {
					$url .= "&&mess=Thêm Ngân Hàng Cho Vay Thành Công";
				} else {
					$url .= "&&mess=Thêm Ngân Hàng Cho Vay Thất Bại, Vui Lòng Thử Lại";
				}
			}
		}
	} else if($acction == "update") {
		if(isset($_POST['loanid']) && isset($_POST['idbloan']) && isset($_POST['bankloadselect']) && isset($_POST['dactinh']) && isset($_POST['dieukien']) && isset($_POST['thutuc'])) {
			$bankid = $_POST['bankloadselect'];
			$bankarr = getBank($bankid);
			if(count($bankarr) > 0) {
				$bankloan = new BankLoan();
				$bankloan -> setID($_POST['idbloan']);
				$bankloan -> setBank($bankid);
				$bankloan -> setDacTinh($_POST['dactinh']);
				$bankloan -> setDieuKien($_POST['dieukien']);
				$bankloan -> setThuTuc($_POST['thutuc']);
				$bankloan -> setBankName($bankarr[0][1]);
				$bankloan -> setBankLogo($bankarr[0][2]);
				$bankloan -> setDatecreate(time());
				
				$url .= "&&as=".$_POST['loanid']."&&adrt=editbankloan&&id=".$_POST['idbloan'];
				if(updateBankLoan($bankloan)) {
					$url .= "&&mess=Cập Nhật Thông Tin Ngân Hàng Cho Vay Thành Công";
				} else {
					$url .= "&&mess=Cập Nhật Thông Tin Ngân Hàng Cho Vay Thất Bại, Vui Lòng Thử Lại";
				}
			}
		}
	}
}
redirect($url);
?>